<?php
class zapAdmin
{
    var $mysql;

    public function __construct()
    {
        $baseURL = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

        session_start();

        if (isset($_SESSION['auth_user']) || isset($_POST['type'])) {
            $config      = parse_ini_file('config.ini', TRUE);
            $this->mysql = $config['mysql'];
            
            if (isset($_POST['type'])) {
                switch ($_POST['type']) {
                case 'login':
                    if ($_POST['username'] == $config['admin']['username'] && $_POST['password'] == $config['admin']['password']) {
                        $_SESSION['auth_user'] = time() + 3600;
                        $result                = $this->query('SHOW TABLES');

                        foreach ($result as $item) {
                            $navigation[] = $item['Tables_in_' . $this->mysql['database']];
                        }

                        require_once 'templates/dashboard.template.php';
                    }
                    break;
                case 'logout':
                    session_destroy();
                    session_unset();

                    require_once 'templates/login.template.php';
                    break;
                case 'select':
                    $result = $this->query('SHOW COLUMNS FROM ' . $_POST['table']);
                    
                    foreach ($result as $item) {
                        $columns[] = $item['Field'];
                    }

                    $rows  = $this->query('SELECT * FROM ' . $_POST['table'] . ' LIMIT ' . $_POST['offset'] .',20');
                    $pages = $this->query('SELECT COUNT(id) as total FROM ' . $_POST['table']);
                    $pages = round(($pages[0]['total'] / 20), PHP_ROUND_HALF_UP);

                    require_once 'templates/data.template.php';
                    break;
                case 'read':
                    $rows = $this->query('SELECT * FROM ' . $_POST['table'] . ' LIMIT ' . ($_POST['offset'] + $_POST['row']) . ',' . ($_POST['row'] + 1));
                    $data = $rows[0];

                    require_once 'templates/detail.template.php';
                    break;
                default:
                    break;
                }
            } else {
                $result = $this->query('SHOW TABLES');

                foreach ($result as $item) {
                    $navigation[] = $item['Tables_in_' . $this->mysql['database']];
                }

                require_once 'templates/dashboard.template.php';
            }
        } else {
            session_destroy();
            session_unset();

            require_once 'templates/login.template.php';
        }
    }
    
    private function query($statement)
    {
        mysql_connect($this->mysql['hostname'],
                      $this->mysql['username'],
                      $this->mysql['password']);
        mysql_select_db($this->mysql['database']);

        $result = mysql_query($statement) or die(mysql_error());
        $data   = array();

        while ($row = mysql_fetch_assoc($result)) {
            $data[] = $row;
        }

        mysql_close();
        return $data;
    }
}